package cm.com.carlamanda.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cm.com.carlamanda.domain.Module;
import cm.com.carlamanda.domain.Page;
import cm.com.carlamanda.service.ModuleService;
import cm.com.carlamanda.util.StringUtil;

import com.alibaba.fastjson.JSONObject;

@Controller
@RequestMapping(value="module")
public class ModuleController {
    @Autowired
    private ModuleService moduleService;
    
    
    
    @RequestMapping(value="add",method=RequestMethod.POST)
    @ResponseBody
    public JSONObject addModule(@RequestBody Module module){
        JSONObject jsonObject = new JSONObject();
        moduleService.add(module);
        jsonObject.put("result",true);
        return jsonObject;
    }
    @RequestMapping(value="get",method=RequestMethod.GET)
    @ResponseBody
    public JSONObject getModule(@RequestBody Module module){
        JSONObject jsonObject = new JSONObject();
        Module mo =moduleService.get(module);
        jsonObject.put("data", mo);
        jsonObject.put("result",true);
        return jsonObject;
    }
    @RequestMapping(value="search",method=RequestMethod.GET)
    @ResponseBody
    public JSONObject searchModule(@RequestParam("page") int page,@RequestParam("pageSize") int pageSize,
            String name ,String description,String projectName,String sortColumn,String sortAsc){
        Module module =new  Module();
        module.setDescription(description);
        module.setName(name);
        module.setProjectName(projectName);
        Page pa=new Page();
        pa.setPage(page);
        pa.setPageSize(pageSize);
        if(StringUtil.isNotEmpty(sortColumn)){
            pa.setSortColumn(sortColumn);
            pa.setSortAsc("desc".equalsIgnoreCase(sortAsc)?"desc":"asc");
        }
        List<Module> list =moduleService.search(module, pa);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("page", pa);
        jsonObject.put("data", list);
        jsonObject.put("result",true);
        return jsonObject;
    }
    
    @RequestMapping(value="del",method=RequestMethod.POST)
    @ResponseBody
    public JSONObject delModule(@RequestParam(value="id") String id){
        Module module = new Module();
        module.setId(id);
        moduleService.del(module);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("result",true);
        return jsonObject;
    }
    
    @RequestMapping(value="update",method=RequestMethod.POST)
    @ResponseBody
    public JSONObject updateModule(@RequestBody Module module){
        moduleService.update(module);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("result",true);
        return jsonObject;
    }
}
